package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private TemplateEngine templateEngine;

    @GetMapping("/")
    public String index(Model model){

        model.addAttribute("list",getData());
        return "index/index";
    }

    //首页静态化
    @GetMapping("/creatHtml")
    @ResponseBody
    public Result creatIndex() throws Exception{

        //模板
        Context context = new Context();
        //数据
        context.setVariable("list",getData());
        Writer writer = new PrintWriter("D:\\Temp\\index.html","utf-8");
        //输出流
        templateEngine.process("index/index",context,writer);
        return Result.ok();
    }

    public  List<Map> getData(){
        List<Map> listMap = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        Map<Long, List<BaseCategoryView>> baseCategoryViewMap = baseCategoryViewList.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        baseCategoryViewMap.forEach((k1,v1) -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("index",k1);
            map1.put("categoryName",v1.get(0).getCategory1Name());
            map1.put("categoryId",v1.get(0).getCategory1Id());
            List<Map> listMap2 = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> baseCategoryViewMap2 = v1.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            baseCategoryViewMap2.forEach((k2,v2) -> {
                Map<Object, Object> map2 = new HashMap<>();
                map2.put("categoryName",v2.get(0).getCategory2Name());
                map2.put("categoryId",v2.get(0).getCategory2Id());
                List<Map> listMap3 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : v1) {
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("categoryName",baseCategoryView.getCategory3Name());
                    map3.put("categoryId",baseCategoryView.getCategory3Id());
                    listMap3.add(map3);
                }
                map2.put("categoryChild",listMap3);
                listMap2.add(map2);
            });
            map1.put("categoryChild",listMap2);
            listMap.add(map1);
        });
        return listMap;
    }
}
